package util

import (
	"os/exec"
	"path/filepath"
)

// Compress7z compress 7z
func Compress7z(workPath string, fileName string) (string, error) {
	fileNameNoExt := fileName[:len(fileName)-len(filepath.Ext(fileName))]
	fileName7z := fileNameNoExt + ".7z"
	command := exec.Command("7z", "a", fileName7z, fileName)
	command.Dir = workPath
	err := command.Run()
	if err != nil {
		log.Errorf("Compress7z cmd error. work path:%s, file name:%s, err:%s", workPath, fileName, err)
		return "", err
	}
	return fileName7z, nil
}

func Compress7zFiles(workPath string, fileName string, fileName1 string) (string, error) {
	log.Errorf(" ====1  2  3:%s, target:%s, err:%s", fileName, fileName1, fileName1)
	fileNameNoExt := fileName[:len(fileName)-len(filepath.Ext(fileName))]
	fileName7z := fileNameNoExt + ".7z"
	command := exec.Command("7z", "a", fileName7z, fileName1)
	command.Dir = workPath
	err := command.Run()
	if err != nil {
		log.Errorf("Compress7z cmd error. work path:%s, file name:%s, err:%s", workPath, fileName, err)
		return "", err
	}
	return fileName7z, nil
}

// Compress7zSourceToTarget compress 7z source to target
func Compress7zSourceToTarget(source string, target string) error {
	command := exec.Command("7z", "a", target, source)
	command.Dir = source
	err := command.Run()
	if err != nil {
		log.Errorf("Compress7zSourceToTarget cmd error. source:%s, target:%s, err:%s", source, target, err)
		return err
	}
	return nil
}
